﻿namespace MDXStudio
{
    using MDXParser;
    using MDXStudio.QueryEditor;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;

    public class MessagesWindow : DockContent
    {
        private IContainer components;
        private System.Windows.Forms.DataGridView dgvMessages;
        private DataGridViewTextBoxColumn dgvMessagesColumn;
        private DataGridViewTextBoxColumn dgvMessagesLine;
        private DataGridViewLinkColumn dgvMessagesLink;
        private DataGridViewTextBoxColumn dgvMessagesMessage;
        private DataGridViewTextBoxColumn dgvSeverity;

        public MessagesWindow()
        {
            this.InitializeComponent();
        }

        private void dgvMessages_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            System.Windows.Forms.DataGridView view = sender as System.Windows.Forms.DataGridView;
            if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
            {
                DataGridViewRow row = view.Rows[e.RowIndex];
                DataGridViewCell cell = row.Cells[e.ColumnIndex];
                if (cell.Tag == null)
                {
                    object tag = row.Tag;
                    if ((tag != null) && (tag.GetType() == typeof(MDXParser.Message)))
                    {
                        MDXParser.Message message = tag as MDXParser.Message;
                        TextBoxSource source = message.Source as TextBoxSource;
                        if ((source == null) && (message.Node != null))
                        {
                            source = message.Node.Source as TextBoxSource;
                        }
                        if (source != null)
                        {
                            MdxEditor textBox = source.TextBox;
                            textBox.SelectAll();
                            textBox.SelectionBackColor = SystemColors.Window;
                            textBox.SelectionStart = message.Location.Position;
                            textBox.SelectionLength = message.Location.Length;
                            textBox.SelectionBackColor = (message.Type == MessageType.Error) ? Color.Red : Color.Yellow;
                            textBox.Focus();
                            textBox.ScrollToCaret();
                            textBox.SelectionLength = 0;
                        }
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            DataGridViewCellStyle style2 = new DataGridViewCellStyle();
            DataGridViewCellStyle style3 = new DataGridViewCellStyle();
            this.dgvMessages = new System.Windows.Forms.DataGridView();
            this.dgvSeverity = new DataGridViewTextBoxColumn();
            this.dgvMessagesLine = new DataGridViewTextBoxColumn();
            this.dgvMessagesColumn = new DataGridViewTextBoxColumn();
            this.dgvMessagesMessage = new DataGridViewTextBoxColumn();
            this.dgvMessagesLink = new DataGridViewLinkColumn();
            ((ISupportInitialize) this.dgvMessages).BeginInit();
            base.SuspendLayout();
            this.dgvMessages.AllowUserToAddRows = false;
            this.dgvMessages.AllowUserToDeleteRows = false;
            style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            style.BackColor = SystemColors.Control;
            style.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            style.ForeColor = SystemColors.WindowText;
            style.SelectionBackColor = SystemColors.Highlight;
            style.SelectionForeColor = SystemColors.HighlightText;
            style.WrapMode = DataGridViewTriState.True;
            this.dgvMessages.ColumnHeadersDefaultCellStyle = style;
            this.dgvMessages.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMessages.Columns.AddRange(new DataGridViewColumn[] { this.dgvSeverity, this.dgvMessagesLine, this.dgvMessagesColumn, this.dgvMessagesMessage, this.dgvMessagesLink });
            style2.Alignment = DataGridViewContentAlignment.MiddleLeft;
            style2.BackColor = SystemColors.Window;
            style2.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            style2.ForeColor = SystemColors.ControlText;
            style2.SelectionBackColor = SystemColors.Highlight;
            style2.SelectionForeColor = SystemColors.HighlightText;
            style2.WrapMode = DataGridViewTriState.False;
            this.dgvMessages.DefaultCellStyle = style2;
            this.dgvMessages.Dock = DockStyle.Fill;
            this.dgvMessages.Location = new Point(0, 0);
            this.dgvMessages.Name = "dgvMessages";
            this.dgvMessages.ReadOnly = true;
            style3.Alignment = DataGridViewContentAlignment.MiddleLeft;
            style3.BackColor = SystemColors.Control;
            style3.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            style3.ForeColor = SystemColors.WindowText;
            style3.SelectionBackColor = SystemColors.Highlight;
            style3.SelectionForeColor = SystemColors.HighlightText;
            style3.WrapMode = DataGridViewTriState.True;
            this.dgvMessages.RowHeadersDefaultCellStyle = style3;
            this.dgvMessages.RowHeadersVisible = false;
            this.dgvMessages.RowHeadersWidth = 20;
            this.dgvMessages.Size = new Size(0x124, 0x111);
            this.dgvMessages.TabIndex = 8;
            this.dgvMessages.CellClick += new DataGridViewCellEventHandler(this.dgvMessages_CellContentClick);
            this.dgvSeverity.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            this.dgvSeverity.HeaderText = "Severity";
            this.dgvSeverity.Name = "dgvSeverity";
            this.dgvSeverity.ReadOnly = true;
            this.dgvSeverity.Width = 70;
            this.dgvMessagesLine.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            this.dgvMessagesLine.HeaderText = "Line";
            this.dgvMessagesLine.Name = "dgvMessagesLine";
            this.dgvMessagesLine.ReadOnly = true;
            this.dgvMessagesLine.Width = 0x34;
            this.dgvMessagesColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            this.dgvMessagesColumn.HeaderText = "Column";
            this.dgvMessagesColumn.Name = "dgvMessagesColumn";
            this.dgvMessagesColumn.ReadOnly = true;
            this.dgvMessagesColumn.Width = 0x43;
            this.dgvMessagesMessage.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dgvMessagesMessage.HeaderText = "Message";
            this.dgvMessagesMessage.Name = "dgvMessagesMessage";
            this.dgvMessagesMessage.ReadOnly = true;
            this.dgvMessagesLink.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            this.dgvMessagesLink.HeaderText = "Link";
            this.dgvMessagesLink.Name = "dgvMessagesLink";
            this.dgvMessagesLink.ReadOnly = true;
            this.dgvMessagesLink.Width = 0x21;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x124, 0x111);
            base.Controls.Add(this.dgvMessages);
            base.HideOnClose = true;
            base.Name = "MessagesWindow";
            base.TabText = "Messages";
            this.Text = "Messages";
            ((ISupportInitialize) this.dgvMessages).EndInit();
            base.ResumeLayout(false);
        }

        internal System.Windows.Forms.DataGridView DataGridView
        {
            get
            {
                return this.dgvMessages;
            }
        }
    }
}

